<?php

if (!defined('FRAMEWORKS_RB_FUNCTIONS_FORMAT')):
define('FRAMEWORKS_RB_FUNCTIONS_FORMAT', true);


// HTML 輸出 - 針對 WYSIWYG 處理
function rb_format_html($html)
{
	$myts =& MyTextSanitizer::getInstance();
	$html = $myts->displayTarea($html, 1, 0, 0, 1, 0);
	return $html;
}

// 美化網址涵式 	
function rb_format_url($add_params=array(), $url='') 
{
	$str_params = array();
	foreach ($add_params as $key => $val) {
		if( ($key == 'x') || ($key == 'y') || ($key == 'start')) continue;
		if( !empty($val) ) {
			if (is_array($val)) {
				foreach ($val as $k => $v) {
					$str_params[] = sprintf('%s[]=%s', $key, $v);
				}
			} else {
				$str_params[] = sprintf('%s=%s', $key, $val);
			}
		}
	}
	$request_url = sprintf('%s', implode('&', $str_params));
	if ($url) {
		$request_url = sprintf('%s?%s', $url, $request_url);
	}

	return $request_url;
}

function rb_nicetime($unix_date)
{
	/*if(empty($date)) {
		return 'No date provided';
	}*/
	$periods = array('秒', '分鐘', '小時', '天', '星期', '個月', '年');
	$lengths = array('60', '60', '24', '7', '4.35', '12');
	$now     = time();
    //$unix_date  = strtotime($date);
	
	// check validity of date
	if(empty($unix_date)) {
		return 'Bad date';
	}
	
	// is it future date or past date
	if($now > $unix_date) {
		$difference     = $now - $unix_date;
		$tense         = '前';
	} else {
		$difference     = $unix_date - $now;
		$tense         = '後';
	}
	
	for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
		$difference /= $lengths[$j];
	}
	
	$difference = round($difference);
	
	/*if($difference != 1) {
		$periods[$j].= 's';
	}*/
	if($difference == 0) {
		return '現在';
	}
	return "$difference $periods[$j]{$tense}";
}

function rb_json_encode($arr) {
	if(function_exists('json_encode')) return json_encode($arr); //Lastest versions of PHP already has this functionality.
	$parts = array();
	$is_list = false;
	
	//Find out if the given array is a numerical array
	$keys = array_keys($arr);
	$max_length = count($arr)-1;
	if(($keys[0] == 0) and ($keys[$max_length] == $max_length)) {//See if the first key is 0 and last key is length - 1
		$is_list = true;
		for($i=0; $i<count($keys); $i++) { //See if each key correspondes to its position
			if($i != $keys[$i]) { //A key fails at position check.
				$is_list = false; //It is an associative array.
				break;
			}
		}
	}
	
	foreach($arr as $key=>$value) {
		if(is_array($value)) { //Custom handling for arrays
			if($is_list) $parts[] = array2json($value); /* :RECURSION: */
			else $parts[] = '"' . $key . '":' . array2json($value); /* :RECURSION: */
		} else {
			$str = '';
			if(!$is_list) $str = '"' . $key . '":';
			//Custom handling for multiple data types
			if(is_numeric($value)) $str .= $value; //Numbers
			elseif($value === false) $str .= 'false'; //The booleans
			elseif($value === true) $str .= 'true';
			else $str .= '"' . addslashes($value) . '"'; //All other things
			// :TODO: Is there any more datatype we should be in the lookout for? (Object?)
			$parts[] = $str;
		}
	}
	$json = implode(',',$parts);
	
	if($is_list) return '[' . $json . ']';//Return numerical JSON
	return '{' . $json . '}';//Return associative JSON
} 
endif;
?>